package proyecto.DataAccessObject;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import proyecto.EntityBeans.Tarea;

@Stateless
@Remote(TareaDAORemote.class)
public class TareaDAO implements TareaDAORemote{

	@PersistenceContext
	EntityManager entityManager;


	public int save (Tarea tarea){		
		try{			
			entityManager.persist(tarea);			
		} 
		catch(Exception e){
			e.printStackTrace();
		}	
		
		return tarea.getIdTarea();
	}
	

	public Tarea findById(int id) {
		return entityManager.find(Tarea.class, id);
		
	}
	
		
	public void merge(Tarea tarea) {
		try{						
			entityManager.merge(tarea);
		} 
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public List findByName(String name)
	{	     
	      return entityManager.createQuery("select t from Tarea t where t.nombre = :nombre").setParameter("nombre", name).getResultList();
	}
	
	public List findAll() {		
		return entityManager.createQuery("from Tarea").getResultList();
	}
	
	public List findByIdList(int id)
	   {	     
	      return entityManager.createQuery("select t from Tarea t where t.idTarea = :idTarea").setParameter("idTarea", id).getResultList();
	   }
	
		
	public void remove(int idTarea){	
		entityManager.remove(entityManager.find(Tarea.class, idTarea));
	}
	

	public void refresh(Tarea tarea){
		entityManager.refresh(tarea);
	}
}
